<?php
require_once( "include/incMain.php" );

$message = "";
$show = "";

$action = $_REQUEST['action']; 
$show = $_REQUEST['show']; 
$id = $_REQUEST['id']; 


if($show == "") $show = "add";



function getContent($id) {
    $retVal = "";
    
    if($id!="") $where = " login.id = $id ";
    else $where = " 1 ";
    
    $sql = "SELECT login.id, login.username, login.password, login.status ,COUNT(characters.id) AS char_sum FROM login LEFT JOIN characters ON characters.login_id = login.id WHERE $where GROUP BY login.id ";
    
	$result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $retVal[] = array( "id" => $row['id'], 
                           "username" => $row['username'], 
                           "password" => $row['password'],
                           "status" => $row['status'],
                           "char_sum" => $row['char_sum'] );
    }
    return $retVal;
}
function addContent() {
    $name = $_REQUEST['loginName'];
    $pass = $_REQUEST['loginPass'];
    $status = $_REQUEST['loginStatus'];
     
    $sql = "INSERT INTO login (username, password, status) VALUES ('$name', '$pass', '$status') ";
    sql($sql);
    return "ADDED";
}
function saveContent() {
    $id = $_REQUEST['id']; 
    $name = $_REQUEST['loginName'];
    $pass = $_REQUEST['loginPass'];
    $status = $_REQUEST['loginStatus'];
     
    $sql = "UPDATE login SET username = '$name', password = '$pass', status = '$status' WHERE id = '$id' ";
    sql($sql);
    return "SAVED";
}
function deleteContent() {
    $id = $_REQUEST['id'];
    sql("DELETE FROM login WHERE id = '$id' ");
}


if($show == "add") $message = "ADD";
if($show == "edit") $message = "EDIT";
if($show == "delete") $message = "DELETE";

if($action == "add") $message = addContent();
if($action == "save") $message = saveContent();
if($action == "delete") $message = deleteContent();
if($action == "back" || $action == "delete") {
    $id = "";
    $show = "add";
}




$smarty = new mySmarty();
$smarty->assign("Path", glPath );
$smarty->assign("id", $id );
$smarty->assign("action", $action );
$smarty->assign("show", $show );
$smarty->assign("message", $message );
$smarty->assign("menue", getMenue(2) );
$smarty->assign("Content", getContent($id) );
$smarty->display(glPath.'/login.htm');


?>
